package org.campuscontent.ui;

import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;
public class Template3 extends TemplateBase
{
	public static int paraID = 3;
	public JCheckBox checkBoxOne;
	public JCheckBox checkBoxTwo;
	public JCheckBox checkBoxThree;
	public JCheckBox checkBoxFour;
	public JTextField textArea1;
	public JTextField textArea2;
	public JTextField textArea3;
	public JTextField textArea4;


	public Template3(MainFrame mf)
	{
		super(mf);

		checkBoxOne = new JCheckBox("");
		checkBoxTwo = new JCheckBox("");
		checkBoxThree = new JCheckBox("");
		checkBoxFour = new JCheckBox("");

		textArea1 = new JTextField();
		textArea2 = new JTextField();
		textArea3 = new JTextField();
		textArea4 = new JTextField();

		if (mf.loidMain.equals("-1") || mf.getCreateTempaletFrom().equals("problemCtrl"))
		{
			sAnswer = new String("none#none#none#none#");
			paraConfig= new String("");
		}
		else
		{
			sAnswer = mf.lobject.getSolution();
			paraConfig = mf.lobject.getConfig();
		}
		initCheckBox();

		textArea1.setBounds(30, 10, 260, 20);
		textArea2.setBounds(330, 10, 260, 20);
		textArea3.setBounds(30, 45, 260, 20);
		textArea4.setBounds(330, 45, 260, 20);

		textArea1.setText("einen Graphen");
		textArea2.setText("einen endlichen Automaten");
		textArea3.setText("einen Baum");
		textArea4.setText("ein Petrinetz");

		left.add(checkBoxOne);
		left.add(checkBoxTwo);
		left.add(checkBoxThree);
		left.add(checkBoxFour);

		left.add(textArea1);
		left.add(textArea2);
		left.add(textArea3);
		left.add(textArea4);
		
		initView();

	}

	public void initCheckBox()
	{
		checkBoxOne.setBounds(5, 5, 20, 30);
		checkBoxTwo.setBounds(305, 5, 20, 30);
		checkBoxThree.setBounds(5, 40, 20, 30);
		checkBoxFour.setBounds(305, 40, 20, 30);

		checkBoxOne.setText("");
		checkBoxTwo.setText("");
		checkBoxThree.setText("");
		checkBoxFour.setText("");
		
		String[] ansArray = sAnswer.split("#");
		if (ansArray[0].equals("one"))
			checkBoxOne.setSelected(true);
		else
			checkBoxOne.setSelected(false);
		if (ansArray[1].equals("two"))
			checkBoxTwo.setSelected(true);
		else
			checkBoxTwo.setSelected(false);
		if (ansArray[2].equals("three"))
			checkBoxThree.setSelected(true);
		else
			checkBoxThree.setSelected(false);
		if (ansArray[3].equals("four"))
			checkBoxFour.setSelected(true);
		else
			checkBoxFour.setSelected(false);

	}

	public void showCheckBox()
	{
		checkBoxOne.setBounds(5, 5, 290, 30);
		checkBoxTwo.setBounds(305, 5, 290, 30);
		checkBoxThree.setBounds(5, 40, 290, 30);
		checkBoxFour.setBounds(305, 40, 290, 30);

		checkBoxOne.setText(textArea1.getText());
		checkBoxTwo.setText(textArea2.getText());
		checkBoxThree.setText(textArea3.getText());
		checkBoxFour.setText(textArea4.getText());
		
		checkBoxOne.setSelected(false);
		checkBoxTwo.setSelected(false);
		checkBoxThree.setSelected(false);
		checkBoxFour.setSelected(false);
	}

	public void initView()
	{
		mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_TEMPLATE);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
	}

	public String getAnswer()
	{
		String sTemp="";
		if (checkBoxOne.isSelected())
			sTemp = sTemp + "one#";
		else
			sTemp = sTemp + "none#";
		if (checkBoxTwo.isSelected())
			sTemp = sTemp + "two#";
		else
			sTemp = sTemp + "none#";
		if (checkBoxThree.isSelected())
			sTemp = sTemp + "three#";
		else
			sTemp = sTemp + "none#";
		if (checkBoxFour.isSelected())
			sTemp = sTemp + "four#";
		else
			sTemp = sTemp + "none#";

		return sTemp;
	}
	
	public String getParaConfig()
	{
		paraConfig = "";
		paraConfig= paraConfig+textArea1.getText() +"#";
		paraConfig= paraConfig+textArea2.getText() +"#";
		paraConfig= paraConfig+textArea3.getText() +"#";
		paraConfig= paraConfig+textArea4.getText() +"#";
		return paraConfig;
	}
	
	public void setParaConfig(String config)
	{
		String[] configArr = config.split("#");
		textArea1.setText(configArr[0]);
		textArea2.setText(configArr[1]);
		textArea3.setText(configArr[2]);
		textArea4.setText(configArr[3]);
		initCheckBox();
//		set the answer
//		String stemp = mf.lobject.getSolution();
//		
//		String[] ansArray =stemp.split("#");
//		if (ansArray[0].equals("one"))
//			checkBoxOne.setSelected(true);
//		else
//			checkBoxOne.setSelected(false);
//		if (ansArray[1].equals("two"))
//			checkBoxTwo.setSelected(true);
//		else
//			checkBoxTwo.setSelected(false);
//		if (ansArray[2].equals("three"))
//			checkBoxThree.setSelected(true);
//		else
//			checkBoxThree.setSelected(false);
//		if (ansArray[3].equals("four"))
//			checkBoxFour.setSelected(true);
//		else
//			checkBoxFour.setSelected(false);
	}

	public boolean setPreview()
	{

		String answer = getAnswer();
		if (answer.equals("none#none#none#none#"))
		{
			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set the answer!");
			return false;
		} else
		{
			mf.lobject.setSolution(answer);
			sAnswer = answer;
		}
		if (!super.setPreview()) return false;
		this.setDividerLocation(Config.POSITION_TO_SHOW_PROBLEM_CONTROLS);
		showCheckBox();
		return true;
	}

	public boolean exitPreview()
	{
		super.exitPreview();
		initCheckBox();
		mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_TEMPLATE);
		return true;
	}

	public String checkAns()
	{
		if (getAnswer().equals(mf.lobject.getSolution())) return "True";
		else
			return "Wrong answer";
	}

	public void showSolution()
	{
		super.showSolution();
		String stemp = mf.lobject.getSolution();
		
		String[] ansArray =stemp.split("#");
		if (ansArray[0].equals("one"))
			checkBoxOne.setSelected(true);
		else
			checkBoxOne.setSelected(false);
		if (ansArray[1].equals("two"))
			checkBoxTwo.setSelected(true);
		else
			checkBoxTwo.setSelected(false);
		if (ansArray[2].equals("three"))
			checkBoxThree.setSelected(true);
		else
			checkBoxThree.setSelected(false);
		if (ansArray[3].equals("four"))
			checkBoxFour.setSelected(true);
		else
			checkBoxFour.setSelected(false);

	}
	
	public boolean updateToLObjectNew()
	{
		//first check if the user have set the 
		sAnswer = getAnswer();
		if (sAnswer.equals("none#none#none#none#"))
		{
			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), "You should set the answer!");
			return false;
		} else
		{
			super.updateToLObjectNew();
			return true;
		}
	}
	public int getId()
	{
		return paraID;
	}
	
}
